#!/usr/bin/perl

require "./dftcfg-lib.pl";

my (@md, $i, $rqrb, @line, $tableng);

&header($text{'index_title'}, "", "index", 0, 1);

@md = &get_md();

print "<form name=form_index action=action.cgi method=post onsubmit='document.forms[0].dftbtn.disabled=true;'>\n";
print "<table class=mode1 border=0 width=100% border=0 cellspacing=0>\n";

print "<tr><td valign=top width=80%>";

print "<table class=mode1 border=0 width=100% border=0 cellspacing=0>\n";

print "<tr class=title1><th width=30% align=left>$text{'th_mdname'}</th><th width=20% align=left>$text{'th_isrb'}</th><th width=50% align=left>$text{'th_disp'}</th></tr>\n";

my $strcls;
$tableng = 0;

for($i = 0; $i < @md; $i++)
{
	if($i % 2)
	{
		$strcls = "class=mode2";
	}
	else
	{
		$strcls = "class=mode1";
	}

	if($config{$md[$i]})
	{
		@line = split(":", $config{$md[$i]});
		if(@line[0] > 0)
		{
			$rqrb = "rq_yes";
		}
		else
		{
			$rqrb = "rq_no";
		}
	}

	print "<tr $strcls>";
	print "<td><input type=checkbox name=checkmd value=\"$md[$i]\" onclick=\"document.forms[0].checkall.checked = false;\">$text{'md_'.$md[$i]}</td><td>$text{$rqrb}</td><td>$text{'disp_'.$md[$i]}</td>";
	print "</tr>";
	$tableng++;
}

if($tableng % 2)
{
	$strcls = "class=mode2";
}
else
{
	$strcls = "class=mode1";
}

print "<tr $strcls><td><input type=checkbox name=checkall value=\"all\" onclick='check_selectall($tableng);'><b>$text{'md_all'}</td><td><b>$text{'rq_yes'}</td><td><b>$text{'disp_all'}</td></tr>";

print "</table>";

print "</td><td valign=top align=center>";

print "<table>";

print "<tr><td align=center><input type=submit class=button name=dftbtn value=\"$text{'btn_dft'}\" onclick='return on_dftbtn_click($tableng, \"$text{'error_msg_noselect_item'}\", \"$text{'index_dftbtn_msg'}\", \"$text{'error_msg_rbt'}\");'></td></tr>";
print "<tr><td align=center><input type=submit class=button name=confbak value=\"$text{'btn_bak'}\" onclick='return on_confbak_click();'></td></tr>";
print "<tr><td align=center><input type=submit class=button name=confrst value=\"$text{'btn_rst'}\" onclick='return on_confrst_click();'></td></tr>";
print "</table>";

print "</td></tr>";

print "</table>\n";
print "</form>";

print <<EOF;
<script>
function on_confbak_click()
{
	document.form_index.action = "download_cfg.cgi";
}
function on_confrst_click()
{
	document.form_index.action = "upload_cfg.cgi";
}
function check_selectall(tableng)
{	
	var i;
	if(tableng > 1)
	{
		for(i = 0; i < tableng; i++)
		{	
			if(document.forms[0].checkall.checked)
			{
				document.forms[0].checkmd[i].checked = true;
			}
			else
			{
				document.forms[0].checkmd[i].checked = false;
			}
		}
	}
}

function on_dftbtn_click(length, error_msg, dftbtn_msg, msg_rbt)
{
	var i;
	var find = 0;
        var rbt = 0;

	if(length < 2)
	{
		if(length < 1)
		{
			return false;
		}
		else
		{
			if(document.forms[0].checkmd.checked)
			{
				find = 1;
			}
		}
	}
	else
	{
		for(i = 0; i < length; i++)
		{
			if(document.forms[0].checkmd[i].checked)
			{
				find = 1;
                                if(document.forms[0].checkmd[i].value == "share" || document.forms[0].checkmd[i].value == "network" || document.forms[0].checkmd[i].value == "naslog" || document.forms[0].checkmd[i].value == "servermin")
                                {
                                          rbt = 1;
                                }
			}
		}
	}

	if(find == 0)
	{
		alert(error_msg);
		return false;
	}

        if(!confirm(dftbtn_msg))
        {
                return false;
        }

        if(rbt)
        {
                alert(msg_rbt);
        }
}

</script>
EOF

&footer("/", $text{'index'});
